﻿package 
{
	import flash.display.MovieClip;
	import flash.events.Event;
		
	/** The highest level container for this project, a task wraps all items. */
	public class Section extends MovieClip
	{
		// private variables
		private var buildMode:Boolean, isFinalFinal:Boolean;
		private var currentItem:int=0;
		
		// private objects
		private var xml:XML;
		private var itemsxml:XMLList;
		private var item:GeometryGameItem;
		private var shapeGarden:ShapeGarden;
		
		public function Section(xml:XML, isFinalFinal:Boolean, buildMode:Boolean=false)
		{
			this.xml = xml;
			this.isFinalFinal = isFinalFinal;
			this.buildMode = buildMode;
			this.addEventListener(Event.ADDED, handleAdded);
		}
		private function handleAdded (event:Event):void
		{
			this.removeEventListener(Event.ADDED, handleAdded);
			this.graphics.drawRect(0,0,this.parent.width, this.parent.height);
			
			itemsxml = xml..item.(attribute("complete") != "true");
			newItem();
		}
		/** Open a new item */
		private function newItem():void
		{
			if (currentItem < itemsxml.length())
			{
				item = new GeometryGameItem(itemsxml[currentItem], currentItem, buildMode);
				item.addEventListener(Event.COMPLETE, handleItemComplete);
				item.addEventListener(Event.CANCEL, handleItemCancel);
				this.addChild(item);
			} else
			{
				this.dispatchEvent(new Event(Event.COMPLETE));				
			}			
		}
		/** The item is complete, iterate and try another */
		private function handleItemComplete(event:Event):void
		{
			item.removeEventListener(Event.COMPLETE, handleItemComplete);
			item.removeEventListener(Event.CANCEL, handleItemCancel);
			// update xml
			var node:XML, pnode:XML;
			var isFirst:Boolean, isFinal:Boolean;
			if (xml..summary_data..child_shape.length()>0){isFirst=false;} else {isFirst=true;}
			if (currentItem == itemsxml.length()-1){isFinal=true;} else {isFinal=false;}
			
			if (xml.child("summary_data").length() == 0)
			{
				node = new XML();
				node = <summary_data> </summary_data>;
				pnode = new XML();
				pnode = <child_shapes> </child_shapes>;
				node.appendChild(pnode);
				pnode = new XML();
				pnode = <obstacles> </obstacles>;
				node.appendChild(pnode);
				xml.appendChild(node);
			}
			// add child nodes
			for each (var cnode:XML in item.xml..child_shapes[0]..child_shape)
			{
				xml.child("summary_data")[0].child("child_shapes")[0].appendChild(cnode.copy());
			}
			ShapeProductionTask(parent.parent).saveXML();			
			currentItem++;
			//trace(xml.child("child_shapes"));
			// are we going to the garden?
			if (item.GARDEN_TIME >= 0)
			{
				shapeGarden = new ShapeGarden(this.width, this.height, item.MENU_HEIGHT, xml.child("summary_data")[0], item.GARDEN_TIME, isFirst, isFinal);
				shapeGarden.addEventListener(Event.COMPLETE, handleZooCompleted);
				this.removeChild(item);
				this.addChild(shapeGarden);
			}
			else
			{
				this.removeChild(item);
				newItem();
			}
		}
			private function handleZooCompleted (event:Event):void
			{
				shapeGarden.removeEventListener(Event.COMPLETE, handleZooCompleted);
				this.removeChild(shapeGarden);
				newItem();
			}
		/** The item is cancelled, go to previous */
		private function handleItemCancel(event:Event):void
		{
			item.removeEventListener(Event.COMPLETE, handleItemComplete);
			item.removeEventListener(Event.CANCEL, handleItemCancel);
			this.removeChild(item);
			if (currentItem > 0) currentItem--;
			newItem();
		}

	}
}